package com.sam.im.sms.index.activitys;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sam.im.greendao.DaoSession;
import com.sam.im.sms.R;
import com.sam.im.sms.app.MyApp;
import com.sam.im.sms.base.BaseActivity;
import com.sam.im.sms.beans.ThreadDetailBean;
import com.sam.im.sms.entity.InterceptEntity;
import com.sam.im.sms.index.adapters.InterceptDetailsAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 描述
 * liuyucheng
 * Created By 刘育成 on 2019/6/10.
 */
public class InterceptDetailsActivity extends BaseActivity {
    private String[] contactProjection = new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME};
    private TextView mName;
    private LinearLayout mLlContent;
    private RecyclerView mMThreadListView;
    private String phone;
    private DaoSession mDaoSession;
    private List<InterceptEntity> mList;
    private InterceptDetailsAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private Context mContext;
    private final static String AUTHORITY = "com.android.peter.provider";
    private final static Uri STUDENT_URI = Uri.parse("content://" + AUTHORITY + "/intercept");
    private String[] projection = new String[]{"id", "address", "type", "read", "body", "date"};
    private ArrayList<ThreadDetailBean> mDatas;
    @Override
    public int getContentViewId() {
        return R.layout.activity_intercept_details;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        mDaoSession = MyApp.getDaoSession();
        Intent intent = getIntent();
        if (intent != null) {
            phone = intent.getStringExtra("phone");
        }
        initViews();
        praseThreadDetailCursor();
    }


    private void praseThreadDetailCursor() {
        String contactName = null;
        Uri contactNameUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
        Cursor contactNameCursor = getContentResolver().query(contactNameUri, contactProjection, null, null, null);
        if (contactNameCursor != null && contactNameCursor.moveToFirst()) {
            contactName = contactNameCursor.getString(1);
        }
        if (contactName != null) {
            mName.setText(contactName);
        } else {
            mName.setText(phone);
        }
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setStackFromEnd(true);
        mMThreadListView.setLayoutManager(manager);
        Collections.reverse(mDatas);
        mAdapter = new InterceptDetailsAdapter(mContext, mDatas);
        mMThreadListView.setAdapter(mAdapter);
    }

    private void initViews() {
        mContext = this;
        mName = findViewById(R.id.name);
        mLlContent = findViewById(R.id.llContent);
        mMThreadListView = findViewById(R.id.mThreadListView);
        Cursor threadDetailCursor = getContentResolver().query(STUDENT_URI, projection, "address=?", new String[]{phone}, "date desc");
//        mList = mDaoSession.getInterceptEntityDao().queryBuilder().where(InterceptEntityDao.Properties.Address.eq(phone)).list();
        praseThreadDetailCursor(threadDetailCursor);
    }

    private void praseThreadDetailCursor(Cursor threadDetailCursor) {
        mDatas = new ArrayList<ThreadDetailBean>();
        if (threadDetailCursor != null && threadDetailCursor.getCount() > 0) {
            while (threadDetailCursor.moveToNext()) {
                String address = threadDetailCursor.getString(threadDetailCursor.getColumnIndex("address"));
                String body = threadDetailCursor.getString(threadDetailCursor.getColumnIndex("body"));
                String type = threadDetailCursor.getString(threadDetailCursor.getColumnIndex("type"));
                String date = threadDetailCursor.getString(threadDetailCursor.getColumnIndex("date"));

                //type為1是对方的item，为2是自己的itme
                //通过电话获取联系人的姓名
                String contactName = null;
                Uri contactNameUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
                Cursor contactNameCursor = getContentResolver().query(contactNameUri, contactProjection, null, null, null);
                if (contactNameCursor != null && contactNameCursor.moveToFirst()) {
                    contactName = contactNameCursor.getString(1);
                }
                Log.i("lyc", "address" + address + ",body" + body + ",type" + type + ",date" + date + ",contactName" + contactName);
                if (type.equals("1")) {
                    //对方
                    ThreadDetailBean heiBean = null;
                    if (contactName != null) {
                        heiBean = new ThreadDetailBean(contactName, date, body, R.layout.hei_item);
                    } else {
                        heiBean = new ThreadDetailBean(address, date, body, R.layout.hei_item);

                    }
                    mDatas.add(heiBean);
                } else {
                    //自己
                    ThreadDetailBean mBean = new ThreadDetailBean(null, date, body, R.layout.me_item);
                    mDatas.add(mBean);
                }
                if (contactName != null) {
                    mName.setText(contactName);
                } else {
                    mName.setText(address);
                }

            }

        }
    }
}
